package com.hanoi.wiki.manager.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hanoi.wiki.dao.CategoryDAO;
import com.hanoi.wiki.dao.CategorySubTypeDAO;
import com.hanoi.wiki.dao.CategoryTypeDAO;
import com.hanoi.wiki.exception.DataAccessException;
import com.hanoi.wiki.manager.CategoryTypeManager;
import com.hanoi.wiki.model.Category;
import com.hanoi.wiki.model.CategorySubType;
import com.hanoi.wiki.model.CategoryType;
import com.hanoi.wiki.model.json.CategoryTypeJson;

@Component
@Transactional
public class CategoryTypeManagerImpl extends BaseManagerImpl implements CategoryTypeManager{
	
	@Autowired
	private CategoryTypeDAO categoryTypeDAO;
	
	@Autowired
	private CategoryDAO categoryDAO;
	
	@Autowired
	private CategorySubTypeDAO categorySubTypeDAO;

	
	public int save(CategoryTypeJson json) {		
		try {
			CategoryType categoryType = new CategoryType(json);
			
			List<CategorySubType> subTypes = new ArrayList<CategorySubType>();
			CategorySubType subType;
			for(Integer i : json.getCategorySubTypes()) {
				subType = categorySubTypeDAO.get(i);
				if(subType != null) {
					subTypes.add(subType);
				}
			}
			
			List<Category> categories = new ArrayList<Category>();
			Category category;
			for(Integer i : json.getCategories()) {
				category = categoryDAO.get(i);
				if(category != null) {
					categories.add(category);
				}
			}		
			
			categoryType.setCategories(categories);
			categoryType.setCategorySubTypes(subTypes);
			
			return categoryTypeDAO.saveCategoryType(categoryType);			
		} catch (DataAccessException e) {
			log.error("",e);
		}
		return 0;
	}

	
	public void update(CategoryTypeJson json) {
		try {
			CategoryType categoryType = new CategoryType(json);
			
			List<CategorySubType> subTypes = new ArrayList<CategorySubType>();
			CategorySubType subType;
			for(Integer i : json.getCategorySubTypes()) {
				subType = categorySubTypeDAO.get(i);
				if(subType != null) {
					subTypes.add(subType);
				}
			}
			
			List<Category> categories = new ArrayList<Category>();
			Category category;
			for(Integer i : json.getCategories()) {
				category = categoryDAO.get(i);
				if(category != null) {
					categories.add(category);
				}
			}
			
			categoryType.setCategories(categories);
			categoryType.setCategorySubTypes(subTypes);
			categoryType.setId(json.getId());
			
			categoryTypeDAO.update(categoryType);
		} catch (DataAccessException e) {
			log.error("",e);
		}		
	}

	
	public CategoryTypeJson get(Integer id) {
		try {
			return new CategoryTypeJson(categoryTypeDAO.get(id));
		} catch (DataAccessException e) {
			log.error("",e);
		}
		return null;
	}

	
	public List<CategoryTypeJson> getList() {
		List<CategoryTypeJson> ctjList = new ArrayList<CategoryTypeJson>();
		List<CategoryType> ctList = categoryTypeDAO.getList();
		try {
			for (CategoryType categoryType : ctList) {
				ctjList.add(new CategoryTypeJson(categoryType));
			}
		} catch (DataAccessException e) {
			log.error("",e);
		}
		return ctjList;
	}

	
	public void remove(Integer id) {
		try {
			if(categoryTypeDAO.get(id) != null) {
				categoryTypeDAO.remove(id);
			}
		} catch (DataAccessException e) {
			log.error("",e);
		}		
	}
}
